package com.chengshi.shop.controller.admin.shop;

import com.chengshi.shop.controller.common.BaseController;
import com.chengshi.shop.model.system.ShopConfig;
import com.chengshi.shop.service.member.MemberService;
import com.chengshi.shop.service.order.OrderService;
import com.chengshi.shop.service.shop.ShopService;
import com.chengshi.shop.util.EnumUtil;
import com.chengshi.shop.util.MessageUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 首页页面管理
 *
 * @author xuxinlong
 * @version 2017年09月04日
 */
@RestController
@RequestMapping(value = "admin/shop")
public class ShopController extends BaseController {
    @Resource
    private ShopService shopService;
    @Resource
    private OrderService orderService;
    @Resource
    private MemberService memberService;

    /**
     * 商城基本配置设置
     *
     * @return
     */
    @GetMapping(value = "shopConfig")
    public ModelAndView shopConfig() {
        ModelAndView mav = new ModelAndView("admin/shop/shopConfig");
        List<ShopConfig> configList = shopService.getShopConfigList();
        for (ShopConfig config : configList) {
            mav.addObject(config.getKey(), config.getValue());
        }
        return mav;
    }

    /**
     * 保存商城简介
     *
     * @param request
     * @return
     */
    @PostMapping(value = "saveShopConfig")
    public HashMap<String, Object> saveShopConfig(HttpServletRequest request) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            Map<String, String[]> map = request.getParameterMap();
            for (Map.Entry<String, String[]> stringEntry : map.entrySet()) {
                String key = ((Map.Entry) stringEntry).getKey().toString();
                String value = ((String[]) ((Map.Entry) stringEntry).getValue())[0];
                shopService.saveShopConfig(key, value);
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 商家数据报表页面
     *
     * @return
     */
    @GetMapping(value = "shopReport")
    public ModelAndView shopReport() {
        ModelAndView mav = new ModelAndView("admin/shop/shopReport");
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);
        Date weekDay = calendar.getTime();
        mav.addObject("waitPay", orderService.getOrderCountByStatus(EnumUtil.ORDER_STATUS.待付款.getValue().byteValue()));
        mav.addObject("waitDelivery", orderService.getOrderCountByStatus(EnumUtil.ORDER_STATUS.待发货.getValue().byteValue()));
        mav.addObject("waitReceipt", orderService.getOrderCountByStatus(EnumUtil.ORDER_STATUS.待收货.getValue().byteValue()));
        mav.addObject("weekTradeOrder", orderService.getTradeOrderByFromDay(s.format(weekDay)));
        mav.addObject("weekTradeAmount", orderService.getTradeAmountByFromDay(s.format(weekDay)));
        mav.addObject("todayTradeOrder", orderService.getTradeOrderByThatDay(s.format(today)));
        mav.addObject("todayNewMember", memberService.getNewMemberByThatDay(s.format(today)));
        List<String> weekDayList = new ArrayList<>();
        List<Integer> newMemberList = new ArrayList<>();
        List<Integer> tradeOrderList = new ArrayList<>();
        for (int i = -6; i <= 0; i++) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, i);
            String day = s.format(c.getTime());
            newMemberList.add(memberService.getNewMemberByThatDay(day));
            tradeOrderList.add(orderService.getTradeOrderByThatDay(day));
            weekDayList.add(day);
        }
        mav.addObject("weekDayList", weekDayList);
        mav.addObject("newMemberList", newMemberList);
        mav.addObject("tradeOrderList", tradeOrderList);
        return mav;
    }
}
